﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Options;
using System.Net;
using System.Net.Sockets;

namespace BuildHelper.Kontakt
{
    public static class KontaktTools
    {
        public const string ServerName = "Build Helper";

        public static string HostIp
        {
            get { return OptionCache.GetOptionStringValue(Init.InitFormOptionNames.Kontakty_Group.Server.HostIp); }
        }
        public static bool IsServer
        {
            get;
            set;
        }
        public static bool CheckIsServer()
        {
            bool res = false;
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in localIPs)
            {
                if (ip.ToString() == HostIp)
                {
                    res = true;
                    break;
                }
            }
            return res;
        }

        public static int Port
        {
            get { return OptionCache.GetOptionValue(Init.InitFormOptionNames.Kontakty_Group.Server.Port).AsInt(4297); }
        }

        public static void RunServerOrClient()
        {
            //OptionCache.OptionChanged += new OptionChangedEventHandler(OptionCache_OptionChanged);

            if (Program.IsArgPresent(Argumenty.NoServer))
                KontaktTools.IsServer = false;
            else
                KontaktTools.IsServer = CheckIsServer();

            return;

            if (KontaktTools.IsServer)
            {
                //Server.Instance.Run();
                ServerNew.StartServer();
            }
            else
            {
                //Client.Instance.Run();
                ClientNew.StartClient();
            }
        }

        //static void OptionCache_OptionChanged(object optionId)
        //{
        //    if (Init.InitFormOptionNames.Kontakty_Group.Server.HostIp.IsTheSameKey(optionId) ||
        //        Init.InitFormOptionNames.Kontakty_Group.Server.Port.IsTheSameKey(optionId))
        //    {
        //        if (KontaktTools.IsServer)
        //        {
        //            Server.Instance.Stop();
        //            Server.Instance.Port = KontaktTools.Port;
        //            Server.Instance.Address = KontaktTools.HostIp;
        //            Server.Instance.Run();
        //        }
        //        else
        //        {
        //            Client.Instance.Stop();
        //            Client.Instance.Port = KontaktTools.Port;
        //            Client.Instance.Address = KontaktTools.HostIp;
        //            Client.Instance.Run();
        //        }
        //    }
        //}

        public static void RunKontakty()
        {
            if (!Program.IsArgPresent(Argumenty.NoContact))
            {
                try
                {
                    KontaktTools.RunServerOrClient();
                }
                catch (Exception ex)
                {
                    ex.Log();
                }
            }
        }

        public static string GetString(byte[] bytes, int count)
        {
            string s = string.Empty;
            try
            {
                s = Encoding.UTF8.GetString(bytes, 0, count);
            }
            catch { }
            return s;
        }
        public static byte[] GetBytes(string msg)
        {
            byte[] bytes = new byte[0];
            try
            {
                bytes = Encoding.UTF8.GetBytes(msg);
            }
            catch { }
            return bytes;
        }

        public static bool SocketConnected(this Socket s)
        {
            bool part1 = s.Poll(1000, SelectMode.SelectRead);
            bool part2 = (s.Available == 0);
            if (part1 & part2)
                return false;
            else
                return true;
        }
    }

    public static class DefaultMessages2
    {
        public const string LOGIN = "\f1\f";
        public const string ID = "\f2\f";
        public const string MSG_START = "\f3\f";
        public const string MSG_END = "\f4\f";
        public const string MSG_SENDER_ID = "\f5\f";
        public const string MSG_SENDER_NICK = "\f6\f";
        public const string MSG_RECEIVER_ID = "\f7\f";
        public const string MSG_START_QUERY = "\f8\f";
        public const string MSG_START_QUERY_RESULT = "\f9\f";
        public const string MSG_START_INFO = "\f10\f";
        public const string MSG_START_BEEP = "\f11\f";
        public const string MSG_SNAPSHOT = "\f12\f";
        public const string MSG_GET_APP_VER = "\f13\f";
        public const string MSG_BEEP = "\f14\f";
        public const string UNKNOWN_MESSAGE = "\f15\f";
    }

    public static class DefaultMessages
    {
        public const string MSG_START_QUERY = "QUERY";
        public const string MSG_START_QUERY_RES = "QUERY_RES";

        public const string MSG_START_INFO = "START_INFO";
       // public const string MSG_START_INFO_RES = "START_INFO_RES";
        
        public const string MSG_GET_APP_VER = "GET_APP_VER";
        public const string MSG_GET_APP_VER_RES = "GET_APP_VER_RES";

        public const string MSG_GET_LOG = "MSG_GET_LOG";
        public const string MSG_GET_LOG_RES = "MSG_GET_LOG_RES";
        

        public const string UNKNOWN_MESSAGE = "UNKNOWN_MESSAGE";
    }
}
